import router from "../router";
import store from "@/store";

/**
 * 扁平化数组
 * @param arr {Array}
 */
export function flatten(arr) {
    return arr.reduce((result, item) => {
        return result.concat(item, (Array.isArray(item.children) ? flatten(item.children) : []));
    }, []);
}


/**
 * 权限函数 判断是否拥有按钮权限
 * @param value {Array}
 * @returns {Boolean}
 */
export const hasPermission = (value = [], name) => {
    const routeName = name ||  router.currentRoute.value.name
    const permissionMap = store.getters.getPermiss
    const permissionArr = permissionMap.get(routeName)
    return flatten(permissionArr || []).some(item => value.indexOf(item.perms) !== -1)
}
/**
 * 过滤请求接口函数
 * @param url {String}
 */
export const requestFilter = (url,name= "") => {
    const routeName = router.currentRoute.value.name || name
    const permissionMap = store.getters.getPermiss
    const permissionArr = permissionMap.get(routeName)
    return flatten(permissionArr || []).some(item => item.url === '/' + url)
}
